package org.ams.log.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.ams.common.annotation.Log;
import org.ams.common.entity.Pager;
import org.ams.common.exception.SystemException;
import org.ams.common.util.Common;
import org.ams.log.entity.LoginLog;
import org.ams.log.entity.OperationLog;
import org.ams.log.service.LoginLogService;
import org.ams.log.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
@Scope("prototype")
@RequestMapping(value = "/log")
public class LogController {

	@Resource
	private LoginLogService loginLogService;
	
	@Autowired
	private OperationLogService operationLogService;
	
	@RequestMapping(value = "/login/main", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String loginMain(HttpServletRequest request) {
		try{
			request.removeAttribute("error");
			return Common.VIEW_PATH + "/log/login/list";
		} catch(Exception e) {
			throw new SystemException(e);
		}
	}
	
	@RequestMapping(value = "/login/list", method = RequestMethod.POST)
	@ResponseBody
	@Log(module="日志信息管理",methods="用户登陆管理-查询")//凡需要处理业务逻辑的.都需要记录操作日志
	public Map<String, Object> loginList(HttpServletRequest request,@RequestParam ("dtGridPager") String dtGridPager) {
		Map<String, Object> params = null;
		// 映射Pager对象
		Pager pager = JSON.parseObject(dtGridPager, Pager.class);
		// 判断是否包含自定义参数
		params = pager.getParameters();
		// 设置分页，page里面包含了分页信息
		Page<Object> page = PageHelper.startPage(pager.getNowPage(),pager.getPageSize(), true);
		List<LoginLog> loginLogList = loginLogService.queryLoginLogListByPage(params);
		params.clear();
		params.put("isSuccess", Boolean.TRUE);
		params.put("nowPage", pager.getNowPage());
		params.put("pageSize", pager.getPageSize());
		params.put("pageCount", page.getPages());
		params.put("recordCount", page.getTotal());
		params.put("startRecord", page.getStartRow());
		//列表展示数据
		params.put("exhibitDatas", loginLogList);
		return params;
	}
	
	@RequestMapping(value = "/main", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String main(HttpServletRequest request) {
		try{
			request.removeAttribute("error");
			return Common.VIEW_PATH + "/log/list";
		} catch(Exception e) {
			throw new SystemException(e);
		}
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	@Log(module="日志信息管理",methods="用户操作管理-查询")//凡需要处理业务逻辑的.都需要记录操作日志
	public Map<String, Object> list(HttpServletRequest request,@RequestParam ("dtGridPager") String dtGridPager) {
		Map<String, Object> params = null;
		// 映射Pager对象
		Pager pager = JSON.parseObject(dtGridPager, Pager.class);
		// 判断是否包含自定义参数
		params = pager.getParameters();
		// 设置分页，page里面包含了分页信息
		Page<Object> page = PageHelper.startPage(pager.getNowPage(),pager.getPageSize(), true);
		List<OperationLog> operationLogList = operationLogService.queryOperationLogListByPage(params);
		params.clear();
		params.put("isSuccess", Boolean.TRUE);
		params.put("nowPage", pager.getNowPage());
		params.put("pageSize", pager.getPageSize());
		params.put("pageCount", page.getPages());
		params.put("recordCount", page.getTotal());
		params.put("startRecord", page.getStartRow());
		//列表展示数据
		params.put("exhibitDatas", operationLogList);
		return params;
	}
}
